package com.onepower.core.modules.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.onepower.core.domain.mapper.MatchScoreInfoMapper;
import com.onepower.core.domain.response.MatchScoreInfo;
import com.onepower.core.exception.BizException;
import com.onepower.core.modules.repository.entity.MatchScore;
import com.onepower.core.modules.repository.mapper.MatchScoreMapper;
import com.onepower.core.modules.service.IMatchScoreService;
import com.onepower.core.result.BizResultCode;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MatchScoreServiceImpl extends ServiceImpl<MatchScoreMapper, MatchScore> implements IMatchScoreService {
    @Override
    public MatchScoreInfo getMatchScore(int matchId, int matchAdminId) {
        MatchScore score = baseMapper.selectScoreByMatchScheduleId(matchAdminId, matchId);
        return MatchScoreInfoMapper.MAPPER.converter(score);
    }

    @Override
    public List<MatchScoreInfo> getMatchScoreList(int matchScheduleId) {
        List<MatchScore> matchScoreList = baseMapper.selectScoreList(matchScheduleId);
        return MatchScoreInfoMapper.MAPPER.converterList(matchScoreList);
    }

    @Override
    public void saveMatchScore(MatchScore matchScore) {
        try {
             baseMapper.insert(matchScore);
        } catch (DuplicateKeyException e) {
            throw new BizException(BizResultCode.MATCH_SCORE_HAS_JUDGE);
        }
    }
}
